

local api = ac.frame
local track_map = {}

--模板动画图标
local temp_icon = {}
for a=1,20 do
    local icon = class.panel.create('',0,0,5,5)
    icon.animation_icon = true
    icon:set_level(9)
    icon:hide()
    temp_icon[a] = icon
end

--获取一个模板图标
function api:copy_temp_icon()
    local icon = table.remove(temp_icon,1)
    if icon then
        local x,y = self:get_real_position()
        icon:set_normal_image(self.normal_image)
        icon:set_real_position(x,y)
        icon:set_control_size(self.w,self.h)
        icon:show()
    end
    return icon
end

function api:add_ani_tween(target,duration,easing)
    local queue = track_map[self.handle] or {}
    local easing = easing or '线性'
    local tween = ac.tween(easing,duration)
    tween.panel = self
    tween.init = function()
        local x1,y1 = self:get_real_position()
        local w1,h1 = self.w,self.h
        local x2,y2 = target[1] or x1,target[2] or y1
        local w2,h2 = target[3] or w1,target[4] or h1
        local start = {x1,y1,w1,h1}
        local target = {x2,y2,w2,h2}
        tween:begin(start)
        tween:after(target)
    end

    table.insert(queue,tween)
    track_map[self.handle] = queue

    return self
end

--[停下所有的动画运行]
function api:stop_ani_tween(target,duration,easing)
    track_map[self.handle] = nil
end

--添加来回动画
function api:add_ani_jump(angle,range,time,easing1,easing2)
    local x1,y1 = self:get_real_position()
    local x2 = x1 + range * math.cos(angle)
    local y2 = y1 + range * math.sin(angle)
    self:add_ani_tween({x2,y2},time/2,easing1)
    self:add_ani_tween({x1,y1},time/2,easing2)
end



ac.hardware:on "界面绘制" (function(_,t)
    for id,queue in pairs(track_map) do
        local tween = queue[1]
        local panel = tween.panel
        if tween.init then
            tween.init()
            tween.init = nil
        end
        local point = tween:update(t)
        panel:set_real_position(point[1],point[2])
        panel:set_control_size(point[3],point[4])
        if tween.clock >= tween.duration then -- the tween has expired
            table.remove(queue,1)
            if #queue==0 then
                track_map[id] = nil
                panel:notify('move_animation_finish')
                if panel.animation_icon then
                    panel:hide()
                    temp_icon[#temp_icon + 1] = panel
                end
            end
        end
    end
end)